package io.summer.gadget.enums;

import io.summer.basis.api.ApiResponse;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.List;


/**
 * @author ml
 */
@RestController
public class EnumController {
  private static final Logger log = LoggerFactory.getLogger(EnumController.class);

  @Value("${summer.gadget.enum-packages:}")
  private List<String> enumPackages;

  private EnumScanner scanner;

  @GetMapping("/gadget/enums")
  public Object enums(String[] names) {
    if(scanner == null) {
      return ApiResponse.fail("no enum definition");
    }
    return ApiResponse.ok(scanner.get(names));
  }

  @PostConstruct
  private void init() {
    if (enumPackages.isEmpty()) {
      log.warn("no enum-packages config, consider define like summer.gadget.enum-packages: p1,p2");
      return;
    }
    scanner = EnumScanner.createEnumScanner(enumPackages);
  }
}
